<?php

require('configs/include.php');

class orden_de_produccion extends super_controller {

//cambiar display por



    public function display() {

        if ($this->error == 1) {
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->temp_aux = 'message.tpl';
        }

        $fecha = date("Y-m-d");


        $options['modelo']['lvl2'] = "all";
        $this->orm->connect();
        $this->orm->read_data(array("modelo"), $options);
        $modelo = $this->orm->get_objects("modelo");
        $this->orm->close();

        $options['orden_produccion']['lvl2'] = "codigo_anterior";
        $this->orm->connect();
        $this->orm->read_data(array("orden_produccion"), $options);
        $orden = $this->orm->get_objects("orden_produccion");
        $this->orm->close();

        //var_dump($modelo);
        $this->engine->assign('orden', $orden);
        $this->engine->assign('modelo', $modelo);
        $this->engine->assign('fecha', $fecha);

        $sdetalles = $_SESSION['GERENTE']['detalles'];
        //$a=unserialize($sdetalles[0]);
        //print_r2($a);
        //echo $a->get('cantidad');
        //var_dump(unserialize($_SESSION['GERENTE']['detalles'][1]));
        $this->engine->assign('detalles', $sdetalles);

        $this->engine->assign('title', 'Generar Orden');
        $this->engine->assign('active', $this->gvar['n_index']);
        $this->engine->display('header.tpl');
        $this->engine->display($this->temp_aux);
        $this->engine->display('orden_de_produccion.tpl');
        $this->engine->display('footer.tpl');
    }

    public function agregar_detalle() {
        $detalle = new detalle($this->post);

        $_SESSION['GERENTE']['detalles'][] = serialize($detalle);
        //$_SESSION['GERENTE']['detalles'] =$detalles;
        //print_r2($detalles);
    }

    public function guardar() {

        if (is_empty($_SESSION['GERENTE']['detalles'])) {
            throw_exception('Detalles vacios');
        }

        //se guarda la orden
        $orden = new orden_produccion($this->post);
        //var_dump($orden);

        $this->orm->connect();
        $this->orm->insert_data("normal", $orden);
        $this->orm->close();
        $this->temp_aux = 'message.tpl';



        //se guardan los detalles

        $detalles = $_SESSION['GERENTE']['detalles'];
        for ($i = 0; $i < sizeof($detalles); $i++) {

            $detalle = unserialize($detalles[$i]);

            $this->orm->connect();
            $this->orm->insert_data("normal", $detalle);
            $this->orm->close();
            $this->temp_aux = 'message.tpl';



            for ($j = 0; $j < $detalle->get('cantidad'); $j++) {
                $guia = new guia();

//se inserta guia

                $this->orm->connect();
                $this->orm->insert_data("normal", $guia);
                $this->orm->close();


//se consulta el codigo de la ultima guia        
                $options['guia']['lvl2'] = "codigo_ultimo";
                $this->orm->connect();
                $this->orm->read_data(array("guia"), $options);
                $codguia = $this->orm->get_objects("guia");
                $this->orm->close();

                $data = new object_standard();
                $data->codigo_guia = $codguia[0]->get('codigo');
                $data->codigo_modelo = $detalle->get('modelo');

//se insertan las motos
                $moto = new moto($data);
                // var_dump($moto);
                $this->orm->connect();
                $this->orm->insert_data("normal", $moto);
                $this->orm->close();

//se crean los reportes
                $datareporte = new object_standard();
                $datareporte->estado_actual = 1;
                $datareporte->descripcion = 'ninguna';
                $datareporte->estado_siguiente = 1;
                $datareporte->fecha = date("Y-m-d");
                $datareporte->empleado = $this->session['empleado']['identificacion'];
                $datareporte->codigo_guia = $codguia[0]->get('codigo');

                $reporte = new reporte($datareporte);
                //var_dump($datareporte);

                $this->orm->connect();
                $this->orm->insert_data("normal", $reporte);
                $this->orm->close();
                //var_dump($datareporte);
            }
        }

        //$this->engine->display('header.tpl');
        $_SESSION['GERENTE']['detalles'] = null;
        $this->engine->assign('type_warning', "success");
        $this->engine->assign('msg_warning', "Orden ingresada correctamente");

        //$this->engine->display($this->temp_aux);
    }

    public function cancelar() {
        session_destroy();
        unset($this->session);

        header('Location: http://localhost/glight/index.php');
    }

    public function run() {

        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
        }

        $this->display();
    }

}

$call = new orden_de_produccion();
$call->run();
?>